﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;

namespace ADB_QuanLyThuVien
{
    public class DAO_Publisher
    {
        public static bool AddPublisher(string tennxb, string diachi, string sodt, string email)
        {
            if (isExist(tennxb) == true)
                return false;
            DTO_NhaXuatBan nxb = new DTO_NhaXuatBan(GetId(), tennxb, diachi, sodt, email);
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            db.Store(nxb);
            db.Close();
            return true;
        }

        private static bool isExist(string tennxb)
        {
            bool kq = false;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            DTO_NhaXuatBan nxb = new DTO_NhaXuatBan(null, tennxb, null, null, null);
            IObjectSet result = db.QueryByExample(nxb);
            if (result.Count > 0)
                kq = true;
            db.Close();
            return kq;
        }

        private static string GetNextId()
        {
            string nextid = "NXB";
            string id = "";
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IObjectSet result = db.QueryByExample(new DTO_NhaXuatBan());
            if (result.Count > 0)
            {
                DTO_NhaXuatBan nxb = (DTO_NhaXuatBan)result[result.Count - 1];
                id = nxb.MaNXB;
                string stt = "";
                for (int i = 3; i < id.Length; ++i)
                    stt += id[i];
                int tmp = Convert.ToInt32(stt) + 1;
                nextid += tmp.ToString("000");
            }
            else
                nextid += "001";
            db.Close();
            return nextid;
        }
        public static string GetId()
        {
            string id = "NXB";
            DateTime now = DateTime.Now;
            id += now.Year.ToString("0000") +now.Month.ToString("00")+now.Day.ToString("00") + now.Hour.ToString("00") + now.Minute.ToString("00") + now.Second.ToString("00");
            return id;
        }
        public static List<DTO_NhaXuatBan> SelectAll()
        {
            List<DTO_NhaXuatBan> lNXB = new List<DTO_NhaXuatBan>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IObjectSet result = db.QueryByExample(new DTO_NhaXuatBan());
            for (int i = 0; i < result.Count; ++i)
                lNXB.Add((DTO_NhaXuatBan)result[i]);
            db.Close();
            return lNXB;
        }
        public static bool RemovePublisher(string idNXB)
        {
            bool kq = true;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            DTO_NhaXuatBan nxb = new DTO_NhaXuatBan(idNXB, null, null, null, null);
            IObjectSet result = db.QueryByExample(nxb);
            DTO_NhaXuatBan tmp = (DTO_NhaXuatBan)result[0];
            db.Delete(tmp);
            db.Close();
            return kq;
        }

        public static DTO_NhaXuatBan GetPublisher(string  idPu)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            DTO_NhaXuatBan temp = new DTO_NhaXuatBan(idPu, null, null, null, null);
            IObjectSet result = db.QueryByExample(temp);
            DTO_NhaXuatBan nxb = (DTO_NhaXuatBan)result.Next();
            db.Close();
            return nxb;
        }

        public static DTO_NhaXuatBan GetPublisherByName(string namePu)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            DTO_NhaXuatBan temp = new DTO_NhaXuatBan(null, namePu, null, null, null);
            IObjectSet result = db.QueryByExample(temp);
            DTO_NhaXuatBan nxb = (DTO_NhaXuatBan)result.Next();
            db.Close();
            return nxb;
        }
    }
}
